package slb

import "sieve_engine/lib/aliyun"

type getAllCACert struct {
	CACertificates struct {
		CACertificate []*CACertificate
	}
}

type CACertificate struct {
	CACertificateId   string
	CACertificateName string
	RegionId          string
	Fingerprint       string
	CommonName        string
	CreateTime        string
	CreateTimeStamp   int64
	ExpireTime        string
	ExpireTimeStamp   int64
	ResourceGroupId   string
}

func GetAllCACertificates() ([]*CACertificate, error) {
	list := []*CACertificate{}
	resp := &getAllCACert{}
	if err := aliyun.ReqApi(*fEndpoint, "DescribeCACertificates", map[string]any{
		"Version":  "2014-05-15",
		"RegionId": aliyun.Region(),
	}, resp); err != nil {
		return nil, err
	}
	if resp.CACertificates.CACertificate != nil {
		for _, r := range resp.CACertificates.CACertificate {
			list = append(list, r)
		}
	}
	return list, nil
}

//

type getAllServerCert struct {
	ServerCertificates struct {
		ServerCertificate []*ServerCertificate
	}
}

type ServerCertificate struct {
	ServerCertificateId     string
	ServerCertificateName   string
	RegionId                string
	Fingerprint             string
	AliCloudCertificateId   string
	AliCloudCertificateName string
	CommonName              string
	CreateTime              string
	CreateTimeStamp         int64
	ExpireTime              string
	ExpireTimeStamp         int64
	IsAliCloudCertificate   int
	ResourceGroupId         string
	SubjectAlternativeNames struct {
		SubjectAlternativeName []string
	}
}

func GetAllServerCertificates() ([]*ServerCertificate, error) {
	list := []*ServerCertificate{}
	resp := &getAllServerCert{}
	if err := aliyun.ReqApi(*fEndpoint, "DescribeServerCertificates", map[string]any{
		"Version":  "2014-05-15",
		"RegionId": aliyun.Region(),
	}, resp); err != nil {
		return nil, err
	}
	if resp.ServerCertificates.ServerCertificate != nil {
		for _, r := range resp.ServerCertificates.ServerCertificate {
			list = append(list, r)
		}
	}
	return list, nil
}
